package com.example.ch07.preferences;

// This file is MainActivity.java

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import com.example.ch07.R;

public class PreferencesSingleActivity extends AppCompatActivity {
    private Resources resources;
    private SharedPreferences prefs;
    private TextView tv = null;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c07_preferences_layout);

        resources = this.getResources();
        // 获得默认的preference对象
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        Log.i("DefaultsActivity", "0");
        // prefs.edit().clear().commit();
        // 在应用目录中设置配置文件缺省值
        PreferenceManager.setDefaultValues(this, R.xml.flightoptions_single,
                false);
        Log.i("DefaultsActivity", "1");

        tv = (TextView) findViewById(R.id.text1);

        setOptionText();
    }
    @Override
    protected void onResume() {
        super.onResume();
        setOptionText();
    }

    private final ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == AppCompatActivity.RESULT_OK) {
                    setOptionText();
                }
            });

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_prefs) {
            Intent intent = new Intent().setClass(this,
                    FlightPreferenceSingleActivity.class);
            mStartForResult.launch(intent);
        }
        return true;
    }

    private void setOptionText() {
        String option = prefs
                .getString(resources
                        .getString(R.string.selected_flight_sort_option), null);
        String[] optionText = resources
                .getStringArray(R.array.flight_sort_options);

        tv.setText("option value is " + option + " ("
                + optionText[Integer.parseInt(option)] + ")");
    }
}